package com.quick.develop.flowable.framework.core;

import com.quick.develop.flowable.constant.BpmConstants;
import com.quick.develop.framework.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.CallActivity;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.delegate.event.FlowableMultiInstanceActivityEvent;
import org.flowable.engine.delegate.event.FlowableProcessStartedEvent;
import org.flowable.engine.delegate.event.impl.FlowableMultiInstanceActivityEventImpl;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author junyuan.liu
 * @description: TODO 全局监听 主要设置实体的一些信息 暂不考虑做其他操作
 * @date 2022/5/20 11:51
 */
@Component
@Slf4j
public class BpmEngineEventListener extends AbstractFlowableEngineEventListener {


    /**
     * @param event 实体
     * @description TODO 多实例监听 如果是启动子流程设置子流程的启动人
     * 子流程启动人设置为局部变量 持久化到数据库 用于子流程查找
     * 暂时不用，子流程启动者为主流程的启动者
     * @author junyuan.liu
     * @date 2022/6/2 15:55
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void multiInstanceActivityStarted(FlowableMultiInstanceActivityEvent event) {
        log.info("====multiInstanceActivityStarted====");
        DelegateExecution execution = ((FlowableMultiInstanceActivityEventImpl) event).getExecution();
        if (execution.getCurrentFlowElement() instanceof CallActivity &&
                !execution.getParent().isMultiInstanceRoot()) {
            execution.setVariableLocal(BpmConstants.SUB_PROCESS_START_USER, SecurityUtils.getUserCode());
        }

        super.multiInstanceActivityStarted(event);

    }

    /**
     * @param event 实体  ExecutionEntityImpl entity
     * @description TODO
     * @author junyuan.liu
     * @date 2022/5/31 14:01
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void processCreated(FlowableEngineEntityEvent event) {
//        ExecutionEntityImpl entity = (ExecutionEntityImpl) event.getEntity();
        super.processCreated(event);
    }


    @Override
    protected void processStarted(FlowableProcessStartedEvent event) {
        System.out.println("===processStarted==");
//        ExecutionEntityImpl entity = (ExecutionEntityImpl) event.getEntity();
        super.processStarted(event);
    }


    /**
     * @param event 实体 HistoricProcessInstanceEntityImpl entity
     * @description TODO
     * @author junyuan.liu
     * @date 2022/5/31 14:00
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void historicProcessInstanceCreated(FlowableEngineEntityEvent event) {
        /*HistoricProcessInstanceEntityImpl entity = (HistoricProcessInstanceEntityImpl) event.getEntity();
        String userCode = SecurityUtils.getUserCode();
        String title = userCode + "发起" +
                entity.getProcessDefinitionName() +
                "流程";
        entity.setName(title);
        entity.setStartUserId(userCode);
        log.info("==historicProcessInstanceCreated==流程创建，设置历史实例信息");*/
        super.historicProcessInstanceCreated(event);
    }


}
